_ = require 'underscore'

{validate} = require '../utils/valid_utils'
{saveComp, removeComp, queryComp, findComp} = require './comp_serv'
{saveValid, removeValid, queryValid, querySomeValid, findByIdValid, updateValid, querySomeByCompIdValid} = require './comp_valid'

# 保存组件
module.exports.save = (req, res, cbf) ->
  # 保存参数
  params =
    comp: req.body?.comp

  # 校验数据
  validResult = validate params, saveValid?.rules, saveValid?.errorTips, true
  if 'success' isnt validResult?.status
    return cbf {msg: validResult}, null

  # 保存组件
  return saveComp params?.comp, cbf

# 删除组件
module.exports.remove = (req, res, cbf) ->
  # 删除条件
  params =
    comp_ids: req.body?.comp_ids

  # 校验数据
  validResult = validate params, removeValid?.rules, removeValid?.errorTips
  if 'success' isnt validResult?.status
    return cbf {msg: validResult}, null

  # 删除组件
  return removeComp {_id: {'$in': params?.comp_ids}}, cbf

# 查询组件 - 分页查询
module.exports.query = (req, res, cbf) ->
  # 查询条件
  params = req?.body
  # 转换类型
  params.pageNum = (parseInt params?.pageNum, 10) or 1
  params.pageSize = (parseInt params?.pageSize, 10) or 10

  # 校验数据
  validResult = validate params, queryValid?.rules, queryValid?.errorTips
  if 'success' isnt validResult?.status
    return cbf {msg: validResult}, null

  # 查询组件
  return queryComp params, cbf

# 查询多个组件 - 不分页，通过传入_id列表
module.exports.querySome = (req, res, cbf) ->
  # 查询条件
  params =
    comp_ids: req.body?.comp_ids

  # 校验数据
  validResult = validate params, querySomeValid?.rules, querySomeValid?.errorTips
  if 'success' isnt validResult?.status
    return cbf {msg: validResult}, null

  # 查询组件
  return findComp {_id: {'$in': params?.comp_ids}}, cbf

# 查询多个组件 - 不分页，通过传入comp_id列表
module.exports.querySomeByCompId = (req, res, cbf) ->
  # 查询条件
  params =
    comp_ids: req.body?.comp_ids

  # 校验数据
  validResult = validate params, querySomeByCompIdValid?.rules, querySomeByCompIdValid?.errorTips
  if 'success' isnt validResult?.status
    return cbf {msg: validResult}, null

  # 查询组件
  return findComp {comp_id: {'$in': params?.comp_ids}}, (error, result) ->
    if error
      return cbf error, null

    obj = {}
    if 0 isnt result?.length
      for item in result
        obj[item.comp_id] = item
      return cbf null, obj
    else
      return cbf null, result

# 查询单个组件
module.exports.findById = (req, res, cbf) ->
  # 组件_id
  comp_id = req.body?.comp_id

  # 查询条件
  params =
    comp_id: comp_id

  # 校验数据
  validResult = validate params, findByIdValid?.rules, findByIdValid?.errorTips
  if 'success' isnt validResult?.status
    return cbf {msg: validResult}, null

  # 查询组件
  return findComp {_id: params?.comp_id}, cbf

# 修改组件
module.exports.update = (req, res, cbf) ->
  # 保存参数
  params =
    comp: req.body?.comp

  # 校验数据
  validResult = validate params, updateValid?.rules, updateValid?.errorTips, true
  if 'success' isnt validResult?.status
    return cbf {msg: validResult}, null

  # 修改字段名
  params.comp._id = params.comp.comp_id
  delete params.comp.comp_id

  # 修改组件
  return saveComp params?.comp, cbf